package com.example.microservice.common.utils;

import com.alibaba.fastjson.JSON;
import com.example.microservice.common.inter.TailRecursion;
import com.example.microservice.common.pojo.TailInvoke;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: caohuijie
 * @Date: 创建日期 2018/11/21
 * @Modified By:
 * @Description: 递归工具类
 */
public class TailRecursionUtils {

    /**
     * 阶乘计算 -- 尾递归解决
     *
     * @param factorial 上一轮递归保存的值
     * @param number 当前阶乘需要计算的数值
     * @return
     */
    public static TailRecursion<Long> factorialTailRecursion(final long factorial, final long number) {
        if (number == 1) {
            return TailInvoke.done(factorial);
        } else {
            return TailInvoke.call(() -> factorialTailRecursion(factorial + number, number - 1));
        }
    }


    public static void main(String[] args) {
        List<Object> test = new ArrayList<>();
        test.add(1);
        test.add("2");
        Object o = JSON.toJSON(test);
        System.out.println(o);

    }
}
